﻿namespace dotnet_notes.Tools
{
    /// <summary>
    /// 图片 EXIF 信息工具类
    /// </summary>
    public static class ImageExifTool
    {
        /// <summary>
        /// 获取图片旋转信息
        /// </summary>
        /// <param name="bytes">图片字节数据</param>
        /// <returns>Orientation 枚举值</returns>
        public static Orientation? GetOrientation(byte[] bytes)
        {
            return ImageFile.FromBuffer(bytes).Properties
                .Get<ExifEnumProperty<Orientation>>(ExifTag.Orientation)?.Value;
        }

        /// <summary>
        /// 设置图片旋转信息并保存为文件
        /// </summary>
        /// <param name="bytes">图片字节数据</param>
        /// <param name="value">Orientation 枚举值</param>
        /// <param name="filePath">文件路径</param>
        public static void SetOrientationAndSave(byte[] bytes, Orientation? value, string filePath)
        {
            var image = ImageFile.FromBuffer(bytes);

            if (value != null) 
            {
                image.Properties.Set(ExifTag.Orientation, value);
            }

            image.Save(filePath);
        }
    }
}
